package ed.hbut.supermarket1.controller;

import ed.hbut.supermarket1.entity.Employee;
import ed.hbut.supermarket1.entity.Result;
import ed.hbut.supermarket1.entity.User;
import ed.hbut.supermarket1.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class EmpController {

    @Autowired
    public EmpService empService;


    /**
     * 查询所有的员工
     * @return
     */
    @GetMapping("/api/employee")
    public Result getAllEmp() {


        List<Employee> list= empService.getAllEmp();

        log.info("employee list :" + list);

        return Result.success(list);
    }


    /**
     * 新增员工
     * @param employee
     * @return
     */
    @PostMapping("/api/employee")
    public Result addEmp(@RequestBody Employee employee) {

        log.info("add employee : {}", employee);
        empService.addEmp(employee);
        return Result.success();
    }

    @PutMapping("/api/employee")
    public Result updateEmp(@RequestBody Employee employee ) {

        log.info("update employee:{}",employee);
        empService.updateEmp(employee);

        return Result.success();
    }

    @DeleteMapping("/api/employee/{id}")
    public Result deleteEmp(@PathVariable int id) {
        empService.deleteEmp(id);
        return  Result.success();
    }


    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    @GetMapping("/api/employee/{id}")
    public Result getEmpBy(@PathVariable int id) {

        log.info("getEmpBy id:{}", id);
        Employee emp=empService.getById(id);

        return Result.success(emp);
    }

}
